<?php

class bandit {

	// Global $post var
	protected static $post;

	static function init() {
		global $post;
		self::$post = $post;		
	}

	static function get_post_content() {
		return self::$post->post_content;
	}

	static function get_lang($option=NULL) {
		$lang = get_option('bandit_language');
		if(isset($lang[$option]))
			$result = $lang[$option];
		return $result;
		
	}

	static function get_post_images($orderby='menu_order',$order='ASC') {
		$images=get_posts('post_type=attachment&post_mime_type=image&numberposts=-1&post_parent='.
			self::$post->ID.'&orderby='.$orderby.'&order='.$order);
		return $images;
	}

	static function time_ago($max=31) {
		// Post date
		$post_date=get_the_time('U');
		// Current time
		$now=current_time('timestamp');
		// Difference
		$date_diff=$now-$post_date;
		// How many days ago?
		$days_ago=(floor($date_diff/(60*60*24)));
		// Set date
		if($days_ago <= 31) {
			$date=human_time_diff($post_date,$now);
			$date.=' '.self::get_lang('post_meta_ago');
		} else {
			$date=the_time('F j, Y');
		}
		// Print date
		echo $date;
	}

	static function post_liked_count() {
		$liked=get_post_meta(self::$post->ID,'_bandit_liked',TRUE);
		echo $liked?$liked:'0';
	}

	static function liked_class() {
		$liked=array();
		if(isset($_COOKIE['_bandit_liked'])) {
			$cookie=base64_decode($_COOKIE['_bandit_liked']);
			$liked=explode('|',$cookie);
		}
		if(in_array(self::$post->ID,$liked))
			echo 'class="active"';
	}
}

?>

<?php while(have_posts()): the_post(); ?>
<?php bandit::init(); ?>
	<article id="entry-<?php the_ID(); ?>" <?php post_class('block entry grid-3'); ?>>
	
		<?php if(is_sticky()) { echo '<div class="sticky-icon"></div>'; } ?>

		<?php if(get_post_format()) { get_template_part('post-formats'); } // POST FORMATS TEMPLATE ?>

		<header>
			<h2 class="entry-title">
				<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
			</h2>
		</header>

		<?php if(bandit::get_post_content() != ""): ?>
		<div class="text">
			<?php the_content(bandit::get_lang('post_read_more')); ?>
		</div>
		<?php endif; ?>

		<ul class="entry-meta clearfix">
			<li class="published"><a href="<?php the_permalink(); ?>" title="Permalink for: <?php the_title(); ?>"><i class="icon"></i><?php bandit::time_ago(); ?></a></li>
			<li class="like-count"><a id="like-<?php the_ID(); ?>" href="#" <?php bandit::liked_class(); ?>>
				<i class="icon"></i><?php bandit::post_liked_count(); ?></a>
			</li>
			<li class="comment-count"><a href="<?php comments_link(); ?>"><i class="icon"></i> <?php comments_number('0','1','%'); ?> </a></li>
		</ul>
	</article>
<?php endwhile;?>